package com.wiloon.java8x.collection;

import com.wiloon.java8x.Foo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * Created by USER on 2015/8/11;
 */
public class CollectionTest {
    private static Logger logger = LoggerFactory.getLogger(CollectionTest.class);

    public static void main(String[] args) {
        System.out.println("start");
        mapTest();
        System.out.println("end");
    }

    public static void test() {

            LinkedBlockingQueue<String> queue = new LinkedBlockingQueue(2);

            queue.add("hello");
            queue.add("world");
            queue.add("yes");

            System.out.println("yes");

    }

    public static void test0() {
        try {
            LinkedBlockingQueue<String> queue = new LinkedBlockingQueue(2);

            queue.put("hello");
            queue.put("world");
            queue.put("yes");

            System.out.println("yes");
        } catch (Exception e) {
// TODO: handle exception
            e.printStackTrace();
        }
    }


    public static void concurrentHashMapTest() {
        Map map = new ConcurrentHashMap();
        map.put(1, new Foo("1", "1"));
        map.put(2, new Foo("2", "2"));
        map.put(3, new Foo("3", "3"));
        Iterator iterator = map.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry entry = (Map.Entry) iterator.next();
            System.out.println(entry.getKey() + "" + entry.getValue());
            map.remove(3);
            //map.put(3, new Foo("3", "3"));
        }
    }

    public static void hashTableTest() {
        Hashtable hashtable = new Hashtable();
        hashtable.put(1, new Foo("1", "1"));
        hashtable.put(2, new Foo("2", "2"));
        hashtable.put(3, new Foo("3", "3"));
        Iterator iterator = hashtable.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry entry = (Map.Entry) iterator.next();
            System.out.println(entry.getKey() + "" + entry.getValue());
            hashtable.remove(entry.getKey());
        }
    }

    public static void listTest() {
        LinkedList<Foo> list = new LinkedList();
        list.add(new Foo("1", "a"));
        list.add(new Foo("2", "2"));
        list.add(new Foo("3", "3"));
        list.add(new Foo("4", "4"));

        for (Foo foo : list) {
            System.out.println(foo.getFoo());
            list.remove(foo);
        }
    }

    public static void mapTest() {
        Map map = new HashMap<>();
        map.put("1", "a");
        map.put("2", "a");
        map.put("3", "a");
        map.put("4", "a");

        System.out.println(map);

        Iterator iter = map.entrySet().iterator();
        while (iter.hasNext()) {
            Map.Entry entry = (Map.Entry) iter.next();
            System.out.println(entry.getKey() + "," + entry.getValue());
            map.remove(entry.getKey());
        }

    }


}
